﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FisheryPlatform.Entity;
using FisheryPlatform.Common.StaticCache;

namespace FisheryPlatform.Bussiness.Option
{
    public class OptionSingleton
    {
        private static OptionSingleton _Option = null;
        private static readonly object locker = new object();

        public static OptionSingleton Instance
        {
            get
            {
                #region
                if (_Option == null)
                {
                    lock (locker)
                    {
                        if (_Option == null)
                        {
                            _Option = new OptionSingleton();
                        }
                    }
                }
                return _Option;
                #endregion
            }
        }

        public List<OptionsEntity> GetAllAutoLoad()
        {
            #region
            List<OptionsEntity> result = new List<OptionsEntity>();
            if (CacheDataUtility.Intance.TryGet<List<OptionsEntity>>("_cache_options_autoload_", out result) == false)
            {
                result = new List<OptionsEntity>();
                var tempList = OptionManager.GetAll();
                tempList.ForEach(item =>
                {
                    if (item.AutoLoad == 1)
                    {
                        result.Add(item);
                    }
                });
                CacheDataUtility.Intance.Set("_cache_options_autoload_", result);
            }
            return result;
            #endregion
        }

        /// <summary>
        /// OptionNameConstants 常量
        /// </summary>
        /// <param name="optionName"></param>
        /// <returns></returns>
        public OptionsEntity Get(string optionName)
        {
            #region
            var autoLoadList = GetAllAutoLoad();
            foreach (var item in autoLoadList)
            {
                if (item.Name == optionName)
                {
                    return item;
                }
            }
            var result = OptionManager.GetByOptionName(optionName);

            return result;
            #endregion
        }

    }
}
